package com.blocks.mq.listener.example.service;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @author lxd
 * @date 2020/12/5 9:46
 */
@Service
public class ExampleXlsxWriterService {

    public void writeExcel(List<List<String>> data, String path) {
        path = path + "-receive.xlsx";

        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet();
        int i = 0;
        for (List<String> rowData : data) {
            Row row = sheet.createRow(i++);
            int j = 0;
            for (String cellData : rowData) {
                Cell cell = row.createCell(j++);
                cell.setCellValue(cellData);
            }
        }
        try (OutputStream fileOut = new FileOutputStream(path)) {
            wb.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
